package com.conferenceroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.conferenceroom.dto.AjaxResult;
import com.conferenceroom.dto.CommentAfterOrder;
import com.conferenceroom.entity.Comment;
import com.conferenceroom.service.CommentService;
import com.conferenceroom.service.RoomService;
import com.conferenceroom.vo.CommentsWithRoomname;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private RoomService roomService;
    @GetMapping
    public AjaxResult getAllComments() {
        List<Comment> comments = commentService.list();
        if (comments == null || comments.isEmpty()) {
            return AjaxResult.fail("无评论");
        }
        List<CommentsWithRoomname> result = comments.stream().map(comment -> {
            // 这里需要获取会议室名称，可能需要调用Service方法获取
            String roomname = (roomService.getById(comment.getRoomId())).getRoomname();
            return new CommentsWithRoomname(
                    roomname,
                    comment.getCommentId(),
                    comment.getOrderId(),
                    comment.getRoomId(),
                    comment.getCommentTime(),
                    comment.getContent(),
                    comment.getRating(),
                    comment.getReplyId(),
                    comment.getCommentType()
            );
        }).collect(Collectors.toList());
        return AjaxResult.success(result);
    }

    @GetMapping("/page")
    public AjaxResult getAllCommentsByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        Page<Comment> commentPage = commentService.page(page);
        return AjaxResult.success(commentPage);
    }


    @GetMapping("/{id}")
    public AjaxResult getCommentById(@PathVariable Integer id) {
        Comment comment = commentService.getById(id);
        return comment != null ? AjaxResult.success(comment) : AjaxResult.fail("评论不存在");
    }

    @PostMapping
    public AjaxResult saveComment(@RequestBody Comment comment) {
        boolean saved = commentService.save(comment);
        return saved ? AjaxResult.success("评论成功") : AjaxResult.fail("评论失败");
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteCommentByid(@PathVariable Integer id) {
        boolean deleted = commentService.removeById(id);
        return deleted ? AjaxResult.success("删除成功") : AjaxResult.fail("删除失败");
    }

    @PostMapping("/page/condition")
    public AjaxResult pageByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody Comment comment
    ) {
        Page<Comment> page = new Page<>(pageNum, pageSize);
        IPage<Comment> result = commentService.pageByCondition(page, comment);
        return AjaxResult.success(result);
    }

    //根据room_id来获取这个room的评论
    @GetMapping("/room/{roomId}")
    public AjaxResult getCommentsByRoomId(@PathVariable Integer roomId) {
        List<Comment> comments = commentService.list(
                new QueryWrapper<Comment>().eq("room_id", roomId)
        );
        return comments != null && !comments.isEmpty()
                ? AjaxResult.success(comments)
                : AjaxResult.success("该会议室暂无评论");
    }

    @PostMapping("/after-order")
    public AjaxResult saveCommentAfterOrder(@RequestBody CommentAfterOrder commentAfterOrder) {
        return commentService.saveCommentAfterOrder(commentAfterOrder);
    }
}
